/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 小程序抽奖-日志列表
 */
import React, { useMemo, useRef } from 'react';
import ProTable from '@ant-design/pro-table';
import DragTable from '@/components/DragTable';
import { Button, message } from 'antd';
import { operationLog } from '@/services/ad/lottery';
import { useDebounceFn } from '@ant-design/pro-utils';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import { actionValueEnum } from '../../utils/ad.lottery';
import styles from './index.less';

const AdvLog: React.FC = () => {
  const formRef = useRef<ActionType>();

  const columns: ProColumns[] = useMemo(
    () => [
      {
        title: '操作时间',
        width: 200,
        dataIndex: 'updated_at',
        align: 'center',
        search: false,
      },
      {
        title: '操作人',
        width: 150,
        dataIndex: 'mobile',
        align: 'center',
        search: false,
      },
      {
        title: '操作内容',
        width: 150,
        dataIndex: 'action',
        align: 'center',
        search: false,
        valueEnum: actionValueEnum,
      },
      {
        title: '广告标题',
        width: 150,
        dataIndex: 'name',
        align: 'center',
        search: false,
      },
    ],
    [],
  );

  const getTableList = async (params: any) => {
    const { current: page, pageSize: size, ...rest } = params;
    const { code, data, msg } = await operationLog({ page, size, ...rest });
    if (code == 0) {
      const { list, count: total } = data;
      return {
        total,
        data: list,
      };
    } else {
      message.error(msg);
      return { data: [] };
    }
  };

  const onRefresh = useDebounceFn(async () => {
    formRef.current?.reload();
  }, 300);

  return (
    <>
      <DragTable>
        <ProTable
          className={styles['kb-lottery-log']}
          actionRef={formRef}
          rowKey='id'
          columns={columns}
          options={{
            fullScreen: false,
            reload: false,
            setting: false,
            density: false,
          }}
          toolBarRender={() => [
            <Button type='primary' onClick={onRefresh.run}>
              刷新
            </Button>,
          ]}
          search={false}
          request={getTableList}
          pagination={{
            pageSize: 20,
            showSizeChanger: false,
          }}
          scroll={{ x: 500 }}
        />
      </DragTable>
    </>
  );
};

export default AdvLog;
